package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heima.common.constants.CacheConstant;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.IWmSensitiveService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author Hulunico
 * @since 2024-08-21
 */
@Service
@RequiredArgsConstructor
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {

    private final WmSensitiveMapper wmSensitiveMapper;
    @Override
    @Cacheable(cacheNames = CacheConstant.SENSITIVE ,key = "#dto.hashCode()")
    public ResponseResult getByPage(SensitiveDto dto) {
        //1.检查参数
        if (ObjectUtils.isEmpty(dto)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.分页查询
        //创建分页对象
        IPage page = new Page(dto.getPage(),dto.getSize());

        //条件
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(WmSensitive::getSensitives,dto.getName());
        wrapper.orderByDesc(WmSensitive::getCreatedTime);

        page(page,wrapper);

        //3.结果返回
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }
}
